Imports DAL
Imports MyClasses
Imports System.Collections
Imports System.Data.DataSet
Imports System.Configuration

Public Class frmManufacturer
    Dim _intManufacturer As Integer
    'Dim intpersonid As Integer
    Dim intAddress As Integer
    Dim intaddrC As Integer
    Dim intaddrS As Integer
    Dim intaddrCi As Integer
    Dim blnFlagAddType As Boolean
    Dim blnCountryFlag As Boolean
    Dim blnStateFlag As Boolean
    Dim blnCityFlag As Boolean
    Dim address(0) As CAddress
    Dim updaddr(0) As CAddress
    Public _intMfctrID As Integer
    Public flgMfctr As Byte

    'KEYDOWN EVENT FOR CONTROLS  
    Private Sub txtFname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFname.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtMname.Focus()
        Else
            If e.Shift = True Then
                If e.KeyCode = Keys.OemQuotes Then
                    e.SuppressKeyPress = True
                End If
                If BCommon.ValidateText(e.KeyCode, "ALF", False) = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        End If
    End Sub

    Private Sub txtManufacturerID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtManufacturerID.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtShortName.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If

    End Sub

    Private Sub txtShortName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtShortName.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtManufacturerName.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALF", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtManufacturerName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtManufacturerName.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtdescr.Focus()
        End If
        If e.Shift = True Then
            If e.KeyCode = Keys.OemQuotes Or e.KeyCode = Keys.OemPeriod Then
                e.SuppressKeyPress = True
            End If
            If BCommon.ValidateText(e.KeyCode, "ALF", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtdescr_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtdescr.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            cmbTitle.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub cmbTitle_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbTitle.KeyDown
        'Rahul_ITS@2007113 : Add following line
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                txtLname.Focus()
            Else
                txtFname.Focus()
            End If
        End If
    End Sub

    'Divya@13/02/2008 : Added for Tab key sequence for Name
    Private Sub cmbTitle_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTitle.Leave
        If Keys.Tab Then
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                txtLname.Focus()
            Else
                txtFname.Focus()
            End If
        End If
    End Sub
    
    Private Sub txtMname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMname.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                cmbAddrType.Focus()
            Else
                txtLname.Focus()
            End If
        Else
            If e.Shift = True Then
                If e.KeyCode = Keys.OemQuotes Or e.KeyCode = Keys.OemPeriod Then
                    e.SuppressKeyPress = True
                End If
                If BCommon.ValidateText(e.KeyCode, "ALF", False) = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        End If
    End Sub

    Private Sub txtMname_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMname.Leave
        'Divya@13/02/2008 : Added for Tab key sequence for Name
        If Keys.Tab Then
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                cmbAddrType.Focus()
            Else
                txtLname.Focus()
            End If
        End If
    End Sub

    Private Sub txtLname_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLname.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            cmbAddrType.Focus()
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                txtFname.Focus()
            Else
                cmbAddrType.Focus()
            End If
        Else
            If e.Shift = True Then
                If e.KeyCode = Keys.OemQuotes Or e.KeyCode = Keys.OemPeriod Then
                    e.SuppressKeyPress = True
                End If
                If BCommon.ValidateText(e.KeyCode, "ALF", False) = False Then
                    e.SuppressKeyPress = True
                End If
            End If
        End If
    End Sub

    Private Sub txtLname_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLname.Leave
        'Divya@13/02/2008 : Added for Tab key sequence for Name
        If Keys.Tab Then
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                txtFname.Focus()
            Else
                cmbAddrType.Focus()
            End If
        End If
    End Sub

    Private Sub cmbAddrType_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbAddrType.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then txtAddr1.Focus()
    End Sub

    Private Sub txtaddr1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAddr1.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            cmbCountry.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub cmbcity_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbCity.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then txtPinCode.Focus()
    End Sub

    Private Sub txtPincode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPinCode.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtPhone1.Focus()
            If e.KeyCode = Keys.Shift Then e.SuppressKeyPress = True
            e.SuppressKeyPress = Not BCommon.ValidateText(e.KeyCode, "NUM", False)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPincode_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPinCode_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPinCode.KeyUp
        Try
            If (BCommon.IsInteger(txtPinCode.Text) = False) Then txtPinCode.Focus() : txtPinCode.Text = ""
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPinCode_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub cmbstate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbState.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then cmbCity.Focus()
    End Sub

    Private Sub cmbCountry_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCountry.GotFocus
        If cmbCountry.SelectedValue = Nothing Then
            cmbCountry.SelectedValue = ConfigurationManager.AppSettings("Country")
        End If
    End Sub

    Private Sub cmbcountry_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbCountry.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then cmbState.Focus()
    End Sub

    Private Sub txtphone1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPhone1.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtPhone2.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "QTY") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtphone1_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPhone1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPhone1.KeyUp
        Try
            If (BCommon.IsInteger(txtPhone1.Text) = False) Then txtPhone1.Focus() : txtPhone1.Text = ""
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPhone1_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtphone2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPhone2.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtFax.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "QTY") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtphone2_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtPhone2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPhone2.KeyUp
        Try
            If (BCommon.IsInteger(txtPhone2.Text) = False) Then txtPhone2.Focus() : txtPhone2.Text = ""
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtPhone2_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtfax_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFax.KeyDown
        Try
            If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
            If e.KeyCode = Keys.Enter Then txtEmail.Focus()
            If e.Shift = True Then
                If (e.KeyCode >= 48 And e.KeyCode <= 48 + 10) Or (e.KeyCode >= 96 And e.KeyCode <= 96 + 10) Then
                    e.SuppressKeyPress = True
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If
            End If
            If BCommon.ValidateMiscell(e.KeyCode, "QTY") = False Then e.SuppressKeyPress = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtfax_KeyDown")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtfax_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFax.KeyUp
        Try
            If (BCommon.IsInteger(txtFax.Text) = False) Then txtFax.Focus() : txtFax.Text = ""
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "txtfax_KeyUp")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub txtemail_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtEmail.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            txtWebSite.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    Private Sub txtwebsite_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtWebSite.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then
            btnSave.Focus()
        Else
            If BCommon.ValidateText(e.KeyCode, "ALFTEXT", False) = False Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub

    'GOTFOCUS EVENT FOR CONTROLS  
    Private Sub txtfname_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFname.GotFocus
        If btnSave.Text = "&Save" Then If txtFname.Text = txtFname.Tag Then txtFname.Text = ""
    End Sub

    Private Sub txtmname_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMname.GotFocus
        If btnSave.Text = "&Save" Then If txtMname.Text = txtMname.Tag Then txtMname.Text = ""
    End Sub

    Private Sub txtlname_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLname.GotFocus
        If btnSave.Text = "&Save" Then If txtLname.Text = txtLname.Tag Then txtLname.Text = ""
    End Sub

    'LOSTFOCUS EVENT FOR CONTROLS  
    Private Sub txtFname_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFname.LostFocus
        txtFname.Text = IIf(Trim(txtFname.Text) = "", txtFname.Tag, Trim(txtFname.Text))
        If txtFname.Text <> "" Or txtFname.Text <> txtFname.Tag Then
            txtFname.Text = UCase(Mid(txtFname.Text, 1, 1)) & Mid(txtFname.Text, 2)
        End If
    End Sub

    Private Sub txtMname_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMname.LostFocus
        txtMname.Text = IIf(Trim(txtMname.Text) = "", txtMname.Tag, Trim(txtMname.Text))
        If txtMname.Text <> "" Or txtMname.Text <> txtFname.Tag Then
            txtMname.Text = UCase(Mid(txtMname.Text, 1, 1)) & Mid(txtMname.Text, 2)
        End If
    End Sub

    Private Sub txtLname_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLname.LostFocus
        txtLname.Text = IIf(Trim(txtLname.Text) = "", txtLname.Tag, Trim(txtLname.Text))
        If txtLname.Text <> "" Or txtLname.Text <> txtFname.Tag Then
            txtLname.Text = UCase(Mid(txtLname.Text, 1, 1)) & Mid(txtLname.Text, 2)
        End If
    End Sub

    'VALIDATEDATA FUNCTION FOR CONTROLS  
    Private Function ValidateData() As Boolean
        Dim count As Integer
        Dim dtmaufactr As New DataTable
        Try
            If Trim(txtManufacturerName.Text) = "" Then
                BCommon.ShowMsgError(10071)
                txtManufacturerName.Focus()
                Return False
            End If

            If Trim(txtShortName.Text) = "" Then
                BCommon.ShowMsgError(10072)
                txtShortName.Focus()
                Return False
            End If

            'Divya 23/10/2007
            If Trim(txtFname.Text) = "" Or Trim(txtFname.Text) = "<First Name>" Then
                BCommon.ShowMsgError(10011)
                txtFname.Focus()
                Return False
            End If
            If Trim(txtLname.Text) = "" Or Trim(txtLname.Text) = "<Last Name>" Then
                BCommon.ShowMsgError(10012)
                txtLname.Focus()
                Return False
            End If
            If Trim(txtPhone1.Text) <> "" Or Trim(txtPhone2.Text) <> "" Then
                If Trim(txtPhone1.Text) = Trim(txtPhone2.Text) Then
                    BCommon.ShowMsgError(10002)
                    txtPhone2.Focus()
                    Return False
                End If
            End If
            '>>End Divya

            If btnSave.Text = "&Save" Then
                dtmaufactr = BManufacturer.GetManufacturer()            'Divya@02/04/2008
                If dtmaufactr.Rows.Count > 0 Then
                    For count = 0 To dtmaufactr.Rows.Count - 1
                        If Trim(txtShortName.Text) = dtmaufactr.Rows(count).Item("mfctrShortName") Then
                            BCommon.ShowMsgError(10419)
                            txtShortName.Focus()
                            Return False
                        End If
                        If Trim(txtManufacturerName.Text) = dtmaufactr.Rows(count).Item("mfctrName") Then
                            BCommon.ShowMsgError(10420)
                            txtShortName.Focus()
                            Return False
                        End If
                    Next
                End If
            End If

            If Trim(txtEmail.Text) <> "" Or Trim(txtWebSite.Text) <> "" Then
                If Trim(txtEmail.Text) = Trim(txtWebSite.Text) Then
                    BCommon.ShowMsgError(10001)
                    txtPhone2.Focus()
                    Return False
                End If
            End If

            If Trim(txtEmail.Text) <> "" Then
                If BCommon.ValidateEmailID(txtEmail.Text) = False Then
                    txtEmail.Focus()
                    Return False
                End If
            End If

            If Trim(txtWebSite.Text) <> "" Then
                If BCommon.ValidateWebAdd(txtWebSite.Text) = False Then
                    txtWebSite.Focus()
                    Return False
                End If
            End If
            'If btnSave.Text = "&Save" Then
            '    If Trim(txtManufacturerID.Text) = "" Then
            '        BCommon.ShowMsgError(10074)
            '        txtManufacturerID.Focus()
            '        Return False
            '    ElseIf BCommon.CheckSeqID(Trim(txtManufacturerID.Text), "MFR") = True Then
            '        BCommon.ShowMsgError(10073)
            '        txtManufacturerID.Text = ""
            '        txtManufacturerID.Focus()
            '        Return False
            '    End If
            'End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
            Return False
        End Try
    End Function

    'Private Function CheckSeqId() As Boolean
    '    Dim blncheck As Boolean = False
    '    blncheck = IIf(BManufacturer.CheckSeqId(Trim(txtManufacturerID.Text)) > 0, True, False)
    '    Return blncheck
    'End Function

    'DISPLAY FUNCTION 
    Private Sub display_data()
        Dim dtbCountry As New DataTable
        Try
            ClearData()
            Call BCommon.SetFormStandards(Me)
            dtbCountry = DAddress.GetCountry()
            BCommon.SetComboValues(cmbTitle, "TITLE")
            BCommon.SetComboValues(cmbAddrType, "ADDRESS_TYPE")
            BCommon.SetComboValues(cmbCountry, dtbCountry, "CountryCD", "Description")
            cmbTitle.SelectedValue = Replace(ConfigurationManager.AppSettings("Title"), ".", "")
            cmbTitle.Tag = cmbTitle.SelectedValue
            cmbCountry.SelectedIndex = -1
            cmbState.SelectedIndex = -1
            cmbCity.SelectedIndex = -1
            BCommon.SetTextReadOnly(txtManufacturerID)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "display_data")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'VIEW FUNCTION 
    Private Sub view_Mfr()
        Dim dtsMfr As New DataSet
        Try
            dtsMfr = BManufacturer.Search_Mfr(_intManufacturer)
            With dtsMfr.Tables(0).Rows(0)
                txtManufacturerID.Text = .Item(0)
                txtManufacturerID.Tag = .Item(0)
                txtManufacturerName.Text = .Item(1)
                txtManufacturerName.Tag = .Item(1)
                txtShortName.Text = .Item(2)
                txtShortName.Tag = .Item(2)
                txtdescr.Text = .Item(3)
                txtdescr.Tag = .Item(3)
                Call view_Person(.Item(4))
                Call view_Addr(.Item(4))
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "view_Mfr()")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub view_Person(ByVal personid As Integer)
        Dim dtsPerson As New DataSet
        Try
            dtsPerson = BManufacturer.Search_person(personid)
            With dtsPerson.Tables(0).Rows(0)
                cmbTitle.SelectedValue = .Item(0)
                cmbTitle.Tag = .Item(0)
                txtFname.Text = .Item(1)
                txtFname.Tag = .Item(1)
                txtMname.Text = .Item(2)
                txtMname.Tag = .Item(2)
                txtLname.Text = .Item(3)
                txtLname.Tag = .Item(3)
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "view_Person()")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub view_Addr(ByVal intPersonID As Integer)
        Dim dtbAddr As New DataTable
        Dim intc As Integer = 0
        Dim intCount, intAddrCount As Integer
        Try
            dtbAddr = DAddress.SearchAddress(intPersonID)
            'For Each row As DataRow In dtbAddr.Rows
            For intAddrCount = 0 To dtbAddr.Rows.Count - 1
                If IsNothing(dtbAddr.Rows(intc)) Then
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If

                For intCount = 0 To cmbAddrType.Items.Count - 1
                    If cmbAddrType.DataSource.Rows(intCount).Item(0).ToString() = dtbAddr.Rows(intc).Item(0).ToString() Then
                        Exit For
                    End If
                Next
                With dtbAddr.Rows(intc)
                    address(intCount) = New CAddress
                    address(intCount).AddressType = .Item(0)
                    address(intCount).Description1 = .Item(1)
                    address(intCount).Description2 = .Item(2)
                    address(intCount).PinCode = .Item(3)
                    address(intCount).City = .Item(4)

                    address(intCount).State = .Item(5)
                    address(intCount).Country = .Item(6)
                    address(intCount).PhoneNo1 = .Item(7)
                    address(intCount).PhoneNo2 = .Item(8)
                    address(intCount).Fax = .Item(11)
                    address(intCount).EmailID = .Item(12)
                    address(intCount).WebSiteAddress = .Item(13)
                    address(intCount).AddressID = .Item(14)
                    intc = intc + 1
                End With
            Next
            If dtbAddr.Rows.Count > 0 Then
                If cmbAddrType.SelectedValue <> dtbAddr.Rows(0).Item(0) Then
                    blnFlagAddType = False
                    cmbAddrType.SelectedValue = dtbAddr.Rows(0).Item(0)
                    blnFlagAddType = True
                End If
                intAddress = cmbAddrType.SelectedIndex
                CCommon.CopyArray(address, updaddr) 'copy address array to updatearray
                If intAddress >= 0 Then
                    Call Getaddress(intAddress)
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "view_Addr()")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'SETDATA FUNCTION 
    Public Function setdata() As CManufacturer
        Dim objMfg As New CManufacturer
        Try
            With objMfg
                .ManufacturerID = _intManufacturer
                .SeqManufacturerID = Trim(txtManufacturerID.Text)
                .Name = Trim(txtManufacturerName.Text)
                .ShortName = Trim(txtShortName.Text)
                .Description = Trim(txtdescr.Text)
            End With
            With objMfg.Person
                If btnSave.Text = "&Save" Then
                    .Title = cmbTitle.SelectedValue
                    .First_Name = IIf(Trim(txtFname.Text) <> txtFname.Tag, Trim(txtFname.Text), "")
                    .Middle_Name = IIf(Trim(txtMname.Text) <> txtMname.Tag, Trim(txtMname.Text), "")
                    .Last_Name = IIf(Trim(txtLname.Text) <> txtLname.Tag, Trim(txtLname.Text), "")
                    .Display_Name = Trim(txtShortName.Text)
                Else
                    .Title = cmbTitle.SelectedValue
                    .First_Name = Trim(txtFname.Text)
                    .Middle_Name = Trim(txtMname.Text)
                    .Last_Name = Trim(txtLname.Text)
                    .Display_Name = Trim(txtShortName.Text)
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
        End Try
        Return objMfg
    End Function

    'SETADDRESS FUNCTION 
    Private Function SetAddress(ByVal IntAddress As Integer) As CAddress()
        Try
            Dim intAddressID As Integer = 0
            If Not IsNothing(address(IntAddress)) Then
                intAddressID = address(IntAddress).AddressID
            End If
            address(IntAddress) = New CAddress
            address(IntAddress).AddressID = intAddressID

            With address(IntAddress)
                .AddressType = cmbAddrType.Items(IntAddress).item(0).ToString()
                .Description1 = Trim(txtAddr1.Text)
                .Country = cmbCountry.SelectedValue
                .State = cmbState.SelectedValue
                .PinCode = IIf(Trim(txtPinCode.Text) <> "", Trim(txtPinCode.Text), 0)
                If cmbCity.SelectedValue = Nothing Then
                    If cmbCity.Text <> "" Then
                        .City = DAddress.SaveCity(.State, .Country, cmbCity.Text, .PinCode)
                    End If
                Else
                    .City = cmbCity.SelectedValue
                End If
                .PhoneNo1 = Trim(txtPhone1.Text)
                .PhoneNo2 = Trim(txtPhone2.Text)
                .Fax = IIf(Trim(txtFax.Text) <> "", Trim(txtFax.Text), 0)
                .EmailID = Trim(txtEmail.Text)
                .WebSiteAddress = Trim(txtWebSite.Text)
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetAddress(intAddressID)")
            Me.Cursor = Cursors.Default
        End Try
        Return address
    End Function

    'SETADDRESS FUNCTION 
    Private Function SetAddress() As CAddress()
        Try
            Dim intAddressID As Integer = 0
            If Not IsNothing(address(cmbAddrType.SelectedIndex)) Then
                intAddressID = address(cmbAddrType.SelectedIndex).AddressID
            End If
            'If Trim(txtAddr1.Text) <> "" Then
            If isAddressChanged() = 1 Then
                address(cmbAddrType.SelectedIndex) = New CAddress
                address(cmbAddrType.SelectedIndex).AddressID = intAddressID
                With address(cmbAddrType.SelectedIndex)
                    .AddressType = Trim(cmbAddrType.SelectedValue)
                    .Description1 = Trim(txtAddr1.Text)
                    .Country = cmbCountry.SelectedValue
                    .State = cmbState.SelectedValue
                    .PinCode = IIf(Trim(txtPinCode.Text) <> "", Trim(txtPinCode.Text), 0)
                    If cmbCity.SelectedValue = Nothing Then
                        If cmbCity.Text <> "" Then
                            .City = DAddress.SaveCity(.State, .Country, cmbCity.Text, .PinCode)
                        End If
                    Else
                        .City = cmbCity.SelectedValue
                    End If
                    .PhoneNo1 = Trim(txtPhone1.Text)
                    .PhoneNo2 = Trim(txtPhone2.Text)
                    .Fax = IIf(Trim(txtFax.Text) <> "", Trim(txtFax.Text), 0)
                    .EmailID = Trim(txtEmail.Text)
                    .WebSiteAddress = Trim(txtWebSite.Text)
                End With
            End If
            'End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetAddress")
            Me.Cursor = Cursors.Default
        End Try
        Return address
    End Function

    Private Function isAddressChanged() As Byte
        If txtAddr1.Text <> txtAddr1.Tag Or Trim(txtPinCode.Text) <> _
        txtPinCode.Tag Or Trim(txtPhone1.Text) <> txtPhone1.Tag Or Trim(txtPhone2.Text) <> txtPhone2.Tag Or txtFax.Text _
        <> txtFax.Tag Or Trim(txtEmail.Text) <> txtEmail.Tag Or Trim(txtWebSite.Text) <> txtWebSite.Tag Then
            Return 1
        Else
            Return 0
        End If
    End Function

    ' FUNCTION TO RETRIEVE ADDRESS 
    Private Sub Getaddress(ByVal intC As Integer)
        Try
            If IsNothing(address(intC)) Then
                Me.Cursor = Cursors.Default
                Exit Sub
            End If
            With address(intC)
                txtAddr1.Text = .Description1
                cmbCountry.SelectedValue = .Country
                cmbState.SelectedValue = .State
                cmbCity.SelectedValue = .City
                txtPinCode.Text = .PinCode
                txtPhone1.Text = .PhoneNo1
                txtPhone2.Text = .PhoneNo2
                txtFax.Text = .Fax
                txtEmail.Text = .EmailID
                txtWebSite.Text = .WebSiteAddress
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Getaddress")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function isupdate() As Boolean
        If Trim(txtShortName.Text) <> Trim(txtShortName.Tag) Or Trim(txtdescr.Text) <> Trim(txtdescr.Tag) _
        Or Trim(txtManufacturerName.Text) <> Trim(txtManufacturerName.Tag) _
        Or (txtFname.Text) <> Trim(txtFname.Tag) _
        Or Trim(txtMname.Text) <> Trim(txtMname.Tag) Or Trim(txtLname.Text) <> Trim(txtLname.Tag) Then
            Return True
        Else
            Return isEqual()
        End If
    End Function

    Private Function isEqual() As Boolean
        Dim intc As New Integer
        Try
            For intc = 0 To address.Length - 1
                If IsNothing(address(intc)) Then Continue For
                If IsNothing(updaddr(intc)) Then Return True : Me.Cursor = Cursors.Default : Exit Function
                With address(intc)
                    If updaddr(intc).City <> .City Or updaddr(intc).Country <> .Country Or _
                    updaddr(intc).Description1 <> .Description1 Or updaddr(intc).Description2 <> .Description2 Or _
                    updaddr(intc).State <> .State Or updaddr(intc).PhoneNo1 <> .PhoneNo1 Or _
                    updaddr(intc).PhoneNo2 <> .PhoneNo2 Or updaddr(intc).PinCode <> .PinCode Or _
                    updaddr(intc).EmailID <> .EmailID Or updaddr(intc).WebSiteAddress <> .WebSiteAddress Then
                        Me.Cursor = Cursors.Default
                        Return True
                        Exit Function
                    End If
                End With
            Next
            Return False
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isequal")
            Me.Cursor = Cursors.Default
            Return False
        End Try
    End Function

    'CLEAR FORM FUNCTION 
    Private Sub ClearData()
        Try
            txtManufacturerID.Text = BCommon.GetSequentialID("MFR")
            txtManufacturerName.Text = ""
            txtManufacturerName.Tag = ""
            txtShortName.Text = ""
            txtdescr.Text = ""
            txtShortName.Tag = ""
            txtFname.Tag = "<First Name>"
            txtMname.Tag = "<Middle Name>"
            txtLname.Tag = "<Last Name>"
            txtFname.Text = txtFname.Tag
            txtMname.Text = txtMname.Tag
            txtLname.Text = txtLname.Tag
            'Divya 29/11/2007
            cmbTitle.SelectedValue = Replace(ConfigurationManager.AppSettings("Title"), ".", "")
            cmbTitle.Tag = cmbTitle.SelectedValue
            If ConfigurationManager.AppSettings("NameSeq") = "TLFM" Then
                txtLname.Location = New Point(244, 130)
                txtFname.Location = New Point(358, 130)
                txtMname.Location = New Point(472, 130)
            End If
            '>>End Divya
            blnFlagAddType = False
            cmbAddrType.SelectedValue = "COM"
            blnFlagAddType = True

            Call clear_addr()

            txtShortName.Focus()
            btnSave.Text = "&Save"
            If flgMfctr = 1 Then btnSearch.Enabled = False Else btnSearch.Enabled = True
            btnDelete.Enabled = False
            'Me.ToolTipNormal.SetToolTip(btnSave, "Save")
            Call ClearAddress()
            'Modified By:-VINIT(2008/01/18)
            Call SetcontrolTag()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ClearData")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    'CLEAR ADDRESS GPBOX FUNCTION 
    Private Sub clear_addr()
        txtAddr1.Text = ""
        cmbCountry.SelectedIndex = -1
        cmbState.SelectedIndex = -1
        cmbCity.SelectedIndex = -1
        txtPinCode.Text = ""
        txtPhone1.Text = ""
        txtPhone2.Text = ""
        txtFax.Text = ""
        txtEmail.Text = ""
        txtWebSite.Text = ""
    End Sub

    'CLEAR ADDRESS ARRAY FUNCTION 
    Private Sub ClearAddress()
        Dim intCount As Integer
        intAddress = 0
        For intCount = 0 To address.Length - 1
            address(intCount) = Nothing
            updaddr(intCount) = Nothing
        Next
    End Sub

    Private Sub change_Country()
        Try
            Dim dtcountry As New DataTable
            intaddrC = cmbCountry.SelectedValue
            dtcountry = DAddress.GetCountryStates(intaddrC)
            blnStateFlag = False
            BCommon.SetComboValues(cmbState, dtcountry, "StateCD", "Description")
            blnStateFlag = True
            If ConfigurationManager.AppSettings("Country") = cmbCountry.SelectedValue Then
                cmbState.SelectedValue = ConfigurationManager.AppSettings("State")
                cmbState.Refresh()
            Else
                cmbCity.Text = ""
                txtPinCode.Text = ""
                cmbState.Text = ""
            End If
            change_State(cmbState.SelectedValue)

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "change_Country")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub change_State(ByVal intaddrS As Integer)
        Try
            Dim dtstate As New DataTable

            intaddrC = cmbCountry.SelectedValue
            dtstate = DAddress.GetStateCity(intaddrS, intaddrC)
            blnCityFlag = False
            BCommon.SetComboValues(cmbCity, dtstate, "CityCD", "Description")
            blnCityFlag = True
            If cmbCity.Items.Count > 0 Then
                If ConfigurationManager.AppSettings("Country") = cmbCountry.SelectedValue And ConfigurationManager.AppSettings("State") = cmbState.SelectedValue Then cmbCity.SelectedValue = ConfigurationManager.AppSettings("City")
            Else
                cmbCity.Text = ""
                txtPinCode.Text = ""
            End If
            cmbCity.Refresh()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "change_State")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub change_City(ByVal intaddrci As Integer)
        Dim intpincode As Integer
        intpincode = DAddress.GetCityPinCode(intaddrci)
        txtPinCode.Text = intpincode
    End Sub

    Private Sub cmbAddrType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbAddrType.SelectedIndexChanged
        If blnFlagAddType = False Then Me.Cursor = Cursors.Default : Exit Sub
        If Trim(txtAddr1.Text) <> "" Then Call SetAddress(intAddress)
        Call clear_addr()
        intAddress = cmbAddrType.SelectedIndex
        If intAddress >= 0 Then
            Call Getaddress(intAddress)
        End If
    End Sub

    Private Sub cmbCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCountry.SelectedIndexChanged
        If blnCountryFlag = False Then Me.Cursor = Cursors.Default : Exit Sub
        Call change_Country()
    End Sub

    Private Sub cmbState_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbState.SelectedIndexChanged
        If blnStateFlag = False Then Me.Cursor = Cursors.Default : Exit Sub
        intaddrS = cmbState.SelectedValue
        Call change_State(intaddrS)
    End Sub

    Private Sub cmbCity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbCity.SelectedIndexChanged
        If blnCityFlag = False Then Me.Cursor = Cursors.Default : Exit Sub
        If cmbCity.Items.Count > 0 Then
            intaddrCi = cmbCity.SelectedValue
            Call change_City(intaddrCi)
        End If
    End Sub

    Private Sub frmManufacturer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isupdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            Call SaveData()
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnClose_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmManufacturer_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Manufacturer")
        e.Cancel = True
    End Sub

    Private Sub frmManufacturer_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Manufacturer")
        End If
    End Sub

    Private Sub frmManufacturer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            display_data()
            ReDim address(cmbAddrType.Items.Count)
            ReDim updaddr(cmbAddrType.Items.Count)

            cmbAddrType.SelectedValue = "COM"
            intAddress = cmbAddrType.SelectedIndex

            blnFlagAddType = True
            blnCountryFlag = True
            blnStateFlag = True
            blnCityFlag = True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmManufacturer_Load")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        Call ClearData()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim form As New frmMasterBrowse
        Try
            Me.Cursor = Cursors.WaitCursor
            form.strSearch = "Manufacturer"
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            '>>END
            form.ShowDialog()
            _intManufacturer = form.strRecordNo
            'Call ClearAddress()
            Call ClearData()
            If _intManufacturer <> 0 Then
                Call view_Mfr()
                btnSave.Text = "&Update"
            End If

            'Modified By:-Vinit(2008/03/27)
            'Enabled the Delete Button on the basis of Financial Year
            If ConfigurationManager.AppSettings("FormMode") = "R" Then
                btnDelete.Enabled = False
            Else
                btnDelete.Enabled = True
            End If
            Call SetcontrolTag()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSearch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If SaveData() Then
                Call ClearData()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSave_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Function SaveData() As Boolean
        Dim blnResult As Boolean
        Try
            If ValidateData() = False Then Me.Cursor = Cursors.Default : Exit Function
            Dim objM As CManufacturer
            objM = setdata()
            If btnSave.Text = "&Save" Then
                blnResult = BManufacturer.SaveManufacturer(objM, "S", SetAddress())
                If blnResult = True Then
                    BCommon.ShowMsgError(3)
                    _intMfctrID = objM.ManufacturerID
                Else
                    BCommon.ShowMsgError(4)
                End If
            Else
                blnResult = BManufacturer.SaveManufacturer(objM, "U", SetAddress())
                If blnResult = True Then
                    BCommon.ShowMsgError(5)
                    _intMfctrID = objM.ManufacturerID
                Else
                    BCommon.ShowMsgError(4)
                End If
            End If
            Return blnResult
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
            Return True
        End Try
    End Function

    Private Sub SetcontrolTag()
        cmbTitle.Tag = cmbTitle.Text
        txtFname.Tag = txtFname.Text
        txtMname.Tag = txtMname.Text
        txtLname.Tag = txtLname.Text
        txtdescr.Tag = txtdescr.Text
        txtShortName.Tag = txtShortName.Text
        txtManufacturerName.Tag = txtManufacturerName.Text
        cmbAddrType.Tag = cmbAddrType.Text
        txtAddr1.Tag = txtAddr1.Text
        cmbCountry.Tag = cmbCountry.Text
        cmbState.Tag = cmbState.Text
        cmbCity.Tag = cmbCity.Text
        txtPinCode.Tag = txtPinCode.Text
        txtPhone1.Tag = txtPhone1.Text
        txtPhone2.Tag = txtPhone2.Text
        txtFax.Tag = txtFax.Text
        txtEmail.Tag = txtEmail.Text
        txtWebSite.Tag = txtWebSite.Text
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If (BManufacturer.IsManufacturerUsed(_intManufacturer)) = 0 Then
                If (BManufacturer.DeleteManufacturer(_intManufacturer)) > 0 Then
                    BCommon.ShowMsgError(10355)
                    txtManufacturerName.Focus()
                    Call ClearData()
                    btnSave.Text = "&Save"
                    btnDelete.Enabled = False
                End If
            Else
                BCommon.ShowMsgError(10354)
                btnDelete.Focus()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
End Class